package com.freen.fadmin.controller.jwt.SysuserApi.sysuser;

import com.freen.fadmin.controller.jwt.JwtSysuserBaseController;
import com.freen.fadmin.entity.sysuser.SysPermission;
import com.freen.fadmin.entity.sysuser.SysRole;
import com.freen.fadmin.repository.sysuser.SysPermissionRepository;
import com.freen.fadmin.repository.sysuser.SysRoleRepository;
import com.freen.fadmin.repository.sysuser.SysUserRepository;
import com.freen.fadmin.util.result.ExceptionMsg;
import com.freen.fadmin.util.result.Response;
import com.freen.fadmin.util.result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/SysApi")
public class SysRoleController extends JwtSysuserBaseController{
    @Autowired
    HttpServletRequest request;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysRoleRepository sysRoleRepository;
    @Autowired
    private SysPermissionRepository sysPermissionRepository;


    //权限
    //restful控制器编写
    //index

    @ResponseBody
    @RequestMapping(value = "/sysRole",method = RequestMethod.GET)
    public Response APIRoleindex(@RequestParam(value = "page", defaultValue = "0") Integer start,
                                 @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                                 @RequestParam(value = "keyword",required = false) String  keyword,
                                 @RequestParam(value = "sort", defaultValue = "id") String  sortq){
        start = start < 0 ? 0 : start;
        Sort sort = Sort.by(Sort.Direction.DESC, sortq);
        Pageable pageable = PageRequest.of(start-1,limit,sort);

        Page<SysRole> page;
        //判断是否有 keyword 如果有就找，如果没有就查全部
        if (null != keyword && !"".equals(keyword)){
            //page = sysUserRepository.findByNameLikeOrPhoneLikeOrCnnameLike(keyword,keyword,keyword,pageable);
            //page = sysUserRepository.findByNameLike("%"+keyword+"%",pageable);
            page = sysRoleRepository.findByCnnameLikeOrRoleLike(
                    "%"+keyword+"%"
                    ,"%"+keyword+"%"
                    ,pageable);

        }else{
            page = sysRoleRepository.findAll(pageable);
        }
        //在没有新知识补充之前，看来只能用hashmap去构造返回的数据了。
        //先构造返回的数据结构
        return new ResponseData(ExceptionMsg.SUCCESS,page);
    }


    //添加角色
    @ResponseBody
    @RequestMapping(value = "/sysRole/create",method = RequestMethod.POST)
    public Response APIsysRolestore(@RequestBody Map<String ,Object> params){
        SysRole sysRole = new SysRole();
        sysRole.setRole("ROLE_"+params.get("role").toString());
        sysRole.setAvailable((Boolean) params.get("available"));
        sysRole.setCnname(params.get("cnname").toString());
        sysRole.setDescription(params.get("description").toString());
        sysRoleRepository.save(sysRole);
        return new ResponseData(ExceptionMsg.SUCCESS,sysRole);
    }

    @ResponseBody
    @RequestMapping(value = "/sysRole/{id}",method = RequestMethod.PUT)
    public Response APIupdate(@PathVariable("id")int id
            ,@RequestBody Map<String ,Object> params){
        SysRole sysRole = sysRoleRepository.findById(id);
        sysRole.setRole(params.get("role").toString());
        sysRole.setAvailable((Boolean) params.get("available"));
        sysRole.setCnname(params.get("cnname").toString());
        if (params.get("description") != null){
            sysRole.setDescription(params.get("description").toString());
        }
        List< String > sysPermissionSTRs = new ArrayList<String>();
        try{
            sysPermissionSTRs = (ArrayList<String>) params.get("permissionSelects");
        }catch (Exception e){
            return result(ExceptionMsg.FAILED);
        }
        List<SysPermission> sysPermissions = new ArrayList<>();
        for (String sysPermissionSTR :sysPermissionSTRs){
            SysPermission sysPermission = sysPermissionRepository.findByName(sysPermissionSTR);
            sysPermissions.add(sysPermission);
        }
        sysRole.setPermissions(sysPermissions);
        sysRoleRepository.save(sysRole);
        return new ResponseData(ExceptionMsg.SUCCESS,sysRole);
    }

    @ResponseBody
    @RequestMapping(value = "/sysRole/Freeze/{id}",method = RequestMethod.POST)
    public Response APIPermissionFreeze(@PathVariable("id")int id){
        //取出用户
        SysRole sysRole = sysRoleRepository.findById(id);
        sysRole.setAvailable(false);
        sysRoleRepository.save(sysRole);
        return result(ExceptionMsg.SUCCESS);
    }

    @ResponseBody
    @RequestMapping(value = "/sysRole/{id}",method = RequestMethod.DELETE)
    public Response APIdestroy(@PathVariable("id")int id){
        sysRoleRepository.deleteById(id);
        return result(ExceptionMsg.SUCCESS);
    }



    //权限总不分页
    @ResponseBody
    @RequestMapping(value = "/sysRoleAll",method = RequestMethod.GET)
    public Response APIsysRoleAll(){
        //获取所有角色数据
        List<SysRole> sysRoles = sysRoleRepository.findAll();
        return new ResponseData(ExceptionMsg.SUCCESS,sysRoles);
    }
}
